
/*
 * ajax请求
 * type 请求方法
 * uri  请求地址
 * param 参数
 * success_cb 成功回调
 * fail_cb 失败回调
 * loading 是否显示加载中..
 */
function ajax(uri,type,param, success_cb, fail_cb, loading) {
    if (!loading) {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
    }
    wx.request({
        url: getApp().globalData.site_url+ uri,
        data: param,
        header: {
            'content-type': 'application/x-www-form-urlencoded;'
        },
        method: type,
        success: function (res) {
            wx.hideLoading();
            if (res.data.code == 200) {
                typeof success_cb == "function" && success_cb(res.data);
            } else {
                if (fail_cb) {
                    typeof fail_cb == "function" && fail_cb(res.data);
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.data.message,
                        showCancel: false,
                        confirmColor: '#92BA00'
                    });
                }
            }
        },
        fail: function () {
            wx.hideLoading();
            wx.showModal({
                title: '提示',
                content: "网络异常，请稍后再试",
                showCancel: false,
            })
            return false;
        }
    })
}


/*
 * 核对是否登录请求
 */
function checkLogin(uri,type,param, forward, success_cb, fail_cb) {  //先调用登录接口登录，登录成功调取回调接口
    var that = this
    var userid = wx.getStorageSync('userid');
    wx.showLoading({
        title: '加载中',
        mask: true,
    })
    if (userid != '') {
        var params = Object.assign({'userid': userid}, param);
        ajax(uri,type,params, success_cb, fail_cb)
    } else {
        alert('请先登录!', function () {
            wx.navigateTo({
                url: '../user/index',
            })
        })
    }
}


function alert(message, success_cb) {
    wx.hideLoading();
    wx.showModal({
        title: '提示',
        content: message,
        showCancel: false,
        confirmColor: '#92BA00',
        success: function (res) {
            if (res.confirm) {
                typeof success_cb == "function" && success_cb(res);
            }
        }
    })
    return false;
}

function toast(msg,icon) {
        wx.showToast({
            title: msg,
            icon: icon,
            duration: 3000
        })
    }


    function fmtDate(timeStamp) {
      var date = new Date();
      date.setTime(timeStamp * 1000);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      return y + '-' + m + '-' + d;
    };

    function formTime(timeStamp) {
      var date = new Date();
      date.setTime(timeStamp * 1000);
      var h = date.getHours();
      var minute = date.getMinutes();
      return [h, minute];
    };   //转为yyyy-mm-dd h:i:s
//开放接口
module.exports.ajax = ajax
module.exports.checkLogin=checkLogin
module.exports.alert=alert
module.exports.toast=toast
module.exports.fmtDate=fmtDate
module.exports.formTime=formTime
